package com.zone.vehicle.sdk.module.superapp.http;

import android.util.Log;

import com.zone.sdk.common.http.Content;
import com.zone.sdk.common.http.OkHttpManager;
import com.zone.sdk.common.http.apimock.ApiMock;
import com.zone.sdk.common.http.apimock.fastjson.FastJsonConverterFactory;
import com.zone.sdk.common.http.apimock.interceptor.MockInterceptor;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * @Author: pkqlqx
 * @CreateDate: 2021/12/5 14:30
 * @Description:
 */
public class SupperAppOkHttpManager extends OkHttpManager {

    private static SupperAppOkHttpManager instance;

    private static HeaderInterceptor headerInterceptor;

    private static BodyInterceptor bodyInterceptor;

    protected OkHttpClient mOkHttpClient;
    protected Retrofit mRetrofit;
    private final String baseUrl;

    private static Map<String, String> header = new HashMap<>();

    private SupperAppOkHttpManager(String baseUrl){
        super();
        headerInterceptor = new HeaderInterceptor();
        bodyInterceptor = new BodyInterceptor();
        this.baseUrl = baseUrl;
    }

    public synchronized static SupperAppOkHttpManager getInstance(String baseUrl){
        if (instance == null){
            instance = new SupperAppOkHttpManager(baseUrl);
        }
        return instance;
    }

    @Override
    public <T> T build(Class<T> service) {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder()
                .connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(READ_TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(WRITE_TIME_OUT, TimeUnit.SECONDS);
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
//        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        builder.addInterceptor(loggingInterceptor);
        //        HeaderInterceptor headerInterceptor = new HeaderInterceptor();
//        builder.addInterceptor(new Interceptor() {
//            @Override
//            public @NotNull Response intercept(@NotNull Chain chain) throws IOException {
//                Request.Builder builder1 = chain.request().newBuilder();
//                Map<String, String> map = new HashMap<>();
//                if (header.size() > 0){
//                    for (String key : header.keySet()) {
//                        builder1.addHeader(key, Objects.requireNonNull(header.get(key)));
//                        map.put(key, Objects.requireNonNull(header.get(key)));
//                    }
//                }
//                builder1.addHeader("Accept-Encoding", "identity");
//                Log.d("Okhttp ", ""+map.toString());
//                return chain.proceed(builder1.build());
//            }
//        });
//        builder.addInterceptor(bodyInterceptor);
        mOkHttpClient = builder.build();

        mRetrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(FastJsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(mOkHttpClient)
                .build();
        return mRetrofit.create(service);
    }
}
